<?php
/**
 * @author 天涯 <mail@phpha.com>
 * @copyright http://blog.phpha.com
 * @link http://blog.phpha.com/vncms
 * @version 1.0
 */
defined('APP_PATH') or exit('Access Denied');
require 'check.php';
class zzcat extends check{
	//构造函数
	public function __construct(){
		parent::__construct();
		global $__action;
		$arr = array();
		$zcrow = $__action == 'upd' ? spClass('m_zzcat')->spLinker()->findAll("id != ".intval($this->spArgs('id')),'odnum ASC,id ASC') : spClass('m_zzcat')->spLinker()->findAll(null,'odnum ASC,id ASC');
		foreach($zcrow as $v){
			$arr[] = array('id'=>$v['id'],'pid'=>$v['pid'],'catename'=>$v['catename'],'odnum'=>$v['odnum'],'modelname'=>$v['model']['modelname']);
		}
		$treeObj = spClass('Tree',array(array('id','pid','catename','format')));
		$this->zzcat = $treeObj->getTree($arr);
	}
	//分类列表
	public function lis(){
		$this->display('zzcat_lis.tpl.php');
	}
	//添加分类
	public function add(){
		$this->table = spClass('m_table')->findAll();
		$this->display('zzcat_add.tpl.php');
	}
	//执行添加
	public function doadd(){
		if(false != spClass('m_zzcat')->spVerifier($this->spArgs()))	$this->error('数据验证错误！',spUrl('zzcat','add'));
		if(false != spClass('m_zzcat')->create($this->spArgs())){
			$this->success('添加栏目成功！',spUrl('zzcat','lis'));
		}else{
			$this->error('添加栏目失败！',spUrl('zzcat','add'));
		}
	}
	//更新排序
	public function updod(){
		$idrow = $this->spArgs('idrow');
		$odnum = $this->spArgs('odnum');
		for($i=0;$i<count($idrow);$i++){
			spClass('m_zzcat')->update(array('id'=>$idrow[$i]),array('odnum'=>(int)$odnum[$i]));
		}
		$this->success('更新排序成功！',spUrl('zzcat','lis'));
	}
	//更新分类
	public function upd(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('zzcat','lis'));
		$this->zcrow = spClass('m_zzcat')->find(array('id'=>$id));
		$this->table = spClass('m_table')->findAll();
		$this->display('zzcat_upd.tpl.php');
	}
	public function doupd(){
		if(false != spClass('m_zzcat')->spVerifier($this->spArgs()))	$this->error('数据验证错误！',spUrl('zzcat','add'));
		$zcrow = $this->spArgs();
		if($zcrow['pid'] == 'default')	unset($zcrow['pid']);
		if(false != spClass('m_zzcat')->update(array('id'=>$zcrow['id']),$zcrow)){
			$this->success('更新栏目成功！',spUrl('zzcat','lis'));
		}else{
			$this->error('更新栏目失败！',spUrl('zzcat','lis'));
		}
	}
	//删除栏目
	public function del(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('zzcat','lis'));
		if(false != spClass('m_zzcat')->find(array('pid'=>$id))){
			$this->error('存在子栏目禁止删除！',spUrl('zzcat','lis'));
		}
		$zcrow = spClass('m_zzcat')->find(array('id'=>$id));
		$tableinfo = spClass('m_table')->find(array('modelid'=>$zcrow['modelid']));
		$tablename = $GLOBALS['G_SP']['db']['prefix'] . $tableinfo['tablename'];
		if(false != spDB($tablename,'id')->find(array('cid'=>$id))){
			$this->error('该栏目非空禁止删除！',spUrl('zzcat','lis'));
		}
		if(false != spClass('m_zzcat')->delete(array('id'=>$id))){
			$this->success('删除栏目成功！',spUrl('zzcat','lis'));
		}else{
			$this->error('删除栏目失败！',spUrl('zzcat','lis'));
		}
	}
}
